import pymysql

class DBUtil:
    conn = None

    @classmethod
    def __get_conn(cls):
        if cls.conn is None:
            cls.conn = pymysql.connect(host="211.103.136.244", port=7061, user="student", password="iHRM_student_2021",
                                       database="test_db", charset="utf8")
        return cls.conn

    @classmethod
    def __close_conn(cls):
        if cls.conn is not None:
            cls.conn.close()
            cls.conn = None

    @classmethod
    def select_one(cls, sql):
        cursor = None
        res=None
        try:
            cls.conn = cls.__get_conn()
            cursor = cls.conn.cursor()
            cursor.execute(sql)
            res = cursor.fetchone()

        except Exception as err:
            print("连接数据库失败：", str(err))
        finally:
            cursor.close()
            cls.__close_conn()
            return res



if __name__ == '__main__':
    print(DBUtil.select_one("select * from t_book;"))